package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.untils.UiNameConst;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 *翻一整页的 
	 * @author liangqian
	 * 
	 */	
	public class PageFullTurning extends Component
	{
		/**
		 * 根据list和最小值 自动计算最大值
		 */
		private var _min:int = 0;
		/**
		 * 当前页数
		 */
		private var _value:int = _min;
		/**
		 * 总页数
		 */
		private var _max:int = _min;
		
		/**
		 * 下一页的回调函数
		 */
		private var onChange:Function;
		
		private var _spacing:int;
		
		public function PageFullTurning(parent:DisplayObjectContainer = null , xpos:int = 0 , ypos:int = 0 , spacingN:int = 5, handler:Function = null)
		{
			super(parent , xpos , ypos);
			onChange = handler;
			spacing = spacingN;
		}
		
		private var preMc1:MovieClip;
		private var nextMc1:MovieClip;
//		private var preMc2:MovieClip;
//		private var nextMc2:MovieClip;
		
		private var preMcArea:Sprite;
		private var nextMcArea:Sprite;
		/**
		 * Overriden in subclasses to create child display objects.
		 */
		override protected function addChildren():void
		{
			preMcArea = new Sprite();
			nextMcArea = new Sprite();
			addChild(preMcArea);
			addChild(nextMcArea);
			preMc1 = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'leftPageWithFullPage')as Class)as MovieClip;
			preMc1.gotoAndStop(4);
			
			nextMc1 =  new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'rightPageWithFullPage')as Class)as MovieClip;
			nextMc1.gotoAndStop(4);
			
//			preMc2 = (new Helpers.leftPageMc) as MovieClip; 
//			preMc2.gotoAndStop(4);
//			
//			nextMc2 = (new Helpers.rightPageMc) as MovieClip;
//			nextMc2.gotoAndStop(4);
			
//			preMc2.x = 5;
//			nextMc2.x = 5;
			preMcArea.addChild(preMc1);
//			preMcArea.addChild(preMc2);
			preMcArea.mouseChildren = false;
			
			nextMcArea.addChild(nextMc1);
//			nextMcArea.addChild(nextMc2);
			nextMcArea.mouseChildren = false;
			max = 0;
		}
		/**
		 * 强制校正当前值
		 */
		private function update():void
		{
			_value =  Math.min(_max , Math.max(_min , _value));
			if(_value > _min)
			{
				if(!preMcArea.buttonMode)
				{
					preMcArea.buttonMode = preMcArea.mouseEnabled = true;
					preMc1.gotoAndStop(1);
	//				preMc2.gotoAndStop(1);
					preMcArea.addEventListener(MouseEvent.ROLL_OVER,preHoverHandler);
					preMcArea.addEventListener(MouseEvent.CLICK,prePageHandler);
				}
			}
			else
			{
				if(preMcArea.buttonMode)
				{
					preMcArea.buttonMode = preMcArea.mouseEnabled = false;
					preMc1.gotoAndStop(4);
	//				preMc2.gotoAndStop(4);
					preMcArea.removeEventListener(MouseEvent.ROLL_OVER,preHoverHandler);
					preMcArea.removeEventListener(MouseEvent.CLICK,prePageHandler);
					preMcArea.removeEventListener(MouseEvent.ROLL_OUT,preOutHandler);
				}
			}
			
			if( _value != _max)
			{
				if(!nextMcArea.buttonMode)
				{
					nextMcArea.buttonMode = nextMcArea.mouseEnabled = true;
					nextMc1.gotoAndPlay(1);
	//				nextMc2.gotoAndPlay(1);
					nextMcArea.addEventListener(MouseEvent.ROLL_OVER,nextHoverHandler);
					nextMcArea.addEventListener(MouseEvent.CLICK,nextPageHandler);
					nextMcArea.addEventListener(MouseEvent.ROLL_OVER,nextHoverHandler);
				}
			}
			else
			{
				if(nextMcArea.buttonMode)
				{
					nextMcArea.buttonMode = nextMcArea.mouseEnabled = false;
					nextMc1.gotoAndStop(4);
	//				nextMc2.gotoAndStop(4);
					nextMcArea.removeEventListener(MouseEvent.ROLL_OVER,nextHoverHandler);
					nextMcArea.removeEventListener(MouseEvent.CLICK,nextPageHandler);
					nextMcArea.removeEventListener(MouseEvent.ROLL_OUT,nextOutHandler);
				}
			}
			
			invalidate();
		}
		
		public function scaleXY(n:Number):void
		{
//			preMc1.scaleX = preMc1.scaleY = preMc2.scaleX = preMc2.scaleY = nextMc1.scaleX = nextMc1.scaleY = nextMc2.scaleX = nextMc2.scaleY = n;
			preMc1.scaleX = preMc1.scaleY = nextMc1.scaleX = nextMc1.scaleY = n;
		}
		
		/**
		 * 设置最小值和当前值,最大值由最小值+可选list的长度决定
		 * @param mi 最小值
		 * @param ma 最大值
		 * @param val 当前
		 */
		public function setMinMaxValue(mi:int , ma:int , val:int):void
		{
			_min = mi;
			_max = Math.max(ma,mi);
			value = val;
		}
		
		override public function setSize(w:int , h:int):void
		{
		}
		
		override public function draw():void
		{
			_width = preMcArea.width * 2 + _spacing;
			nextMcArea.x = _width - preMcArea.width;
			//			super.x = _x - width * .5;
		}
		
		/**
		 * 下一页
		 */
		private function nextPageHandler(e:MouseEvent):void
		{
			//			nextMc.gotoAndStop(3);
			value++;
			_isClickPrePage = false;
			if (onChange is Function)
				onChange(this);
		}
		
		public var _isClickPrePage:Boolean;
		
		/**
		 * 上一页
		 */
		private function prePageHandler(e:MouseEvent):void
		{
			//			preMc.gotoAndStop(3);
			value--;
			_isClickPrePage = true;
			if (onChange is Function)
				onChange(this);
		}
		
		private function preHoverHandler(e:MouseEvent):void
		{
			preMc1.gotoAndStop(2);
//			preMc2.gotoAndStop(2);
			preMcArea.addEventListener(MouseEvent.ROLL_OUT,preOutHandler);
		}
		
		private function nextHoverHandler(e:MouseEvent):void
		{
			nextMc1.gotoAndStop(2);
//			nextMc2.gotoAndStop(2);
			nextMcArea.addEventListener(MouseEvent.ROLL_OUT,nextOutHandler);
		}
		
		private function preOutHandler(e:MouseEvent):void
		{
			preMc1.gotoAndStop(1);
//			preMc2.gotoAndStop(1);
			preMcArea.removeEventListener(MouseEvent.ROLL_OUT,preOutHandler);
		}
		
		private function nextOutHandler(e:MouseEvent):void
		{
			nextMc1.gotoAndStop(1);
//			nextMc2.gotoAndStop(1);
			nextMcArea.removeEventListener(MouseEvent.ROLL_OUT,nextOutHandler);
		}
		
		public function get spacing():int
		{
			return _spacing;
		}
		
		public function set spacing(value:int):void
		{
			_spacing = value;
			invalidate();
		}
		
		private var _offset:int = 1;
		private function get index():int
		{
			return _value + _offset;
		}
		
		/**
		 * 返回当前页数 ∈[_min,_max]
		 */
		public function get value():int
		{
			return _value;
		}
		
		public function set value(num:int):void
		{
			_value = num;
			update();
		}
		
		public function get max():int
		{
			return _max;
		}
		
		public function set max(num:int):void
		{
			setMinMaxValue(_min , num , _value);
		}
		
		public function get min():int
		{
			return _min;
		}
		
		public function set min(num:int):void
		{
			setMinMaxValue(num , _max , _value);
		}
	}
}